﻿using FieldTools.Util;
using RRQMCore;
using RRQMCore.Log;
using RRQMSocket;
using RRQMSocket.Plugins;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FieldTools.RRQMSocket.Plugin
{
    /// <summary>
    /// 自定义RRQM Tcp插件
    /// </summary>
    public class MyTcpPlugin : TcpPluginBase
    {

        /// <inheritdoc/>
        protected override void OnConnected(ITcpClientBase client, RRQMEventArgs e)
        {
            Console.WriteLine($"客户端{client.IP}正在连接：");
        }

        /// <inheritdoc/>
        protected override void OnConnecting(ITcpClientBase client, ClientOperationEventArgs e)
        {
            Console.WriteLine($"客户端{client.IP}即将连接成功：");
            Console.WriteLine(e.ID);
        }

        /// <inheritdoc/>
        protected override void OnDisconnected(ITcpClientBase client, ClientDisconnectedEventArgs e)
        {
            Console.WriteLine($"客户端{client.IP}断开：");
            Console.WriteLine(e.Message);
        }

        /// <inheritdoc/>
        protected override void OnIDChanged(ITcpClientBase client, RRQMEventArgs e)
        {
            Console.WriteLine($"客户端{client.IP}的ID被更改：");
            Console.WriteLine(client.IP);
        }

        /// <inheritdoc/>
        protected override void OnReceivedData(ITcpClientBase client, ReceivedDataEventArgs e)
        {
            Console.WriteLine($"客户端{client.IP}收到数据：");
            Console.WriteLine(Encoding.Default.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len));
            byte[] vs = Encoding.Default.GetBytes("回发数据++++");
            client.Send(vs);
        }

        /// <inheritdoc/>
        protected override void OnSending(ITcpClientBase client, SendingEventArgs e)
        {
            Console.WriteLine($"客户端{client.IP}即将给客户端发送数据：");
            Console.WriteLine(Encoding.Default.GetString(e.Buffer, 0, e.Length));
        }

        /// <inheritdoc/>
        protected override void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                }

                // TODO: 释放未托管的资源(未托管的对象)并重写终结器
                // TODO: 将大型字段设置为 null
                disposedValue = true;
            }
        }
    }
}
